import asyncio
import sys
import os

# Add the project root to the Python path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from tortoise import Tortoise, run_async
from src.backend.models import AirportRWY

async def seed_data():
    await Tortoise.init(
        db_url="sqlite://db.sqlite3",
        modules={"models": ["src.backend.models"]}
    )
    await Tortoise.generate_schemas()

    demo_data = [
        {
            "code": "ZBAD",
            "name": "17L-35R",
            "airport_name": "Beijing Daxing International Airport",
            "longitude_start": 116.415,
            "latitude_start": 39.53,
            "longitude_end": 116.419,
            "latitude_end": 39.49,
        },
        {
            "code": "ZSSS",
            "name": "18L-36R",
            "airport_name": "Shanghai Hongqiao International Airport",
            "longitude_start": 121.33,
            "latitude_start": 31.19,
            "longitude_end": 121.34,
            "latitude_end": 31.22,
        },
        {
            "code": "ZGGG",
            "name": "02L-20R",
            "airport_name": "Guangzhou Baiyun International Airport",
            "longitude_start": 113.30,
            "latitude_start": 23.38,
            "longitude_end": 113.31,
            "latitude_end": 23.41,
        },
        {
            "code": "ZBTJ",
            "name": "16R-34L",
            "airport_name": "Tianjin Binhai International Airport",
            "longitude_start": 117.35,
            "latitude_start": 39.12,
            "longitude_end": 117.36,
            "latitude_end": 39.15,
        },
    ]

    for data in demo_data:
        rwy = await AirportRWY.filter(code=data["code"], name=data["name"]).first()
        if not rwy:
            await AirportRWY.create(**data)
            print(f"Created: {data['airport_name']} - {data['name']}")
        else:
            print(f"Exists: {data['airport_name']} - {data['name']}")

    await Tortoise.close_connections()

if __name__ == "__main__":
    run_async(seed_data())
